<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
<!--      <el-form-item label="部门名称" prop="deptName">-->
<!--        <el-input-->
<!--          v-model="queryParams.deptName"-->
<!--          placeholder="请输入部门名称"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->

      <el-form-item label="筛选最小入职时长（月）与最大入职时长月：" prop="condition">
        <template>
          <el-input-number v-model="minNum" controls-position="right"  :min="0" :max="maxNum !== 0 ? maxNum : null" label="最小入职时长"></el-input-number>
          <el-input-number v-model="maxNum" controls-position="right"  :min="12" label="最大入职时长"></el-input-number>
          <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        </el-form-item>
        </template>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-sort"
          size="mini"
          @click="toggleExpandAll"
        >展开/折叠</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table
      v-if="refreshTable"
      v-loading="loading"
      :data="deptList"
      row-key="deptId"
      :default-expand-all="isExpandAll"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column prop="deptName" label="部门名称" width="260"></el-table-column>
      <el-table-column label="人数" align="center" prop="deptEmpNum">
        <template slot-scope="scope">
          <span>{{ scope.row.deptEmpNum }}</span>
        </template>
      </el-table-column>
    </el-table>

  </div>
</template>

<script>
import { listDept, getDept, delDept, addDept, updateDept, listDeptExcludeChild } from "@/api/system/dept";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { getEmpnumByTimeApi } from "@/api/Empment/DataStatistics";
import 'element-ui/lib/input-number'
export default {
  name: "Dept",
  dicts: ['sys_normal_disable'],
  components: { Treeselect },
  data() {
    return {
      minNum: 0,
      maxNum: 0,
      // getEmpnumByTimeApi的返回数据
      deptData: [],
      // 遮罩层
      loading: false,
      // 显示搜索条件
      showSearch: true,
      // 表格树数据
      deptList: [],
      // 部门树选项
      deptOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否展开，默认全部展开
      isExpandAll: true,
      // 重新渲染表格状态
      refreshTable: true,
      // 查询参数
      queryParams: {
        deptName: undefined,
        deptId: undefined
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {

    /** 查询部门列表 */
    async getList() {
      this.loading = true;
      listDept(this.queryParams).then(response => {
        this.deptList = this.handleTree(response.data, "deptId");
        this.populateEmployeeCount(this.deptList);
        this.loading = false;
      });
    },
    async populateEmployeeCount(deptList) {
      await Promise.all(deptList.map(async (dept) => {
        dept.deptempNum = await getEmpnumByTimeApi(this.minNum,this.maxNum,dept.deptId).then(response => {
          return response.data.deptempNum;
        });
        this.$set(dept, 'deptEmpNum', dept.deptempNum); // 确保 Vue 能够检测到变化
        if (dept.children && dept.children.length > 0) {
          await this.populateEmployeeCount(dept.children); // 递归处理子部门
        }
      }));
    },
    /** 转换部门数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.deptId,
        label: node.deptName,
        children: node.children
      };
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 展开/折叠操作 */
    toggleExpandAll() {
      this.refreshTable = false;
      this.isExpandAll = !this.isExpandAll;
      this.$nextTick(() => {
        this.refreshTable = true;
      });
    }
  }
};
</script>
